from django.http import HttpResponse
import json
import requests
from bs4 import BeautifulSoup
import re

#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def formData(response):
    allData = []
    # 获取sid并且更新返回
    soup = BeautifulSoup(response.text, "html5lib")

    lis = soup.select(".list li")

    for li in lis:
        tempData = resultData()
        try:
            tempData["titleName"] = li.select("p")[0].text.strip()
        except:
            pass
        try:
            tempData["source"] = li.select("ul.opt-bar")[0].text.strip().replace('来源：','');
        except:
            pass
        allData.append(tempData)

    return allData

def getData(keyword):
    headers = {
        'Accept': 'text/plain, */*; q=0.01',
        'Referer': 'http://gongjushu.cnki.net/RBook/Search/SimpleSearch?key=cell&range=TOTAL&opt=0',
        'Origin': 'http://gongjushu.cnki.net',
        'X-Requested-With': 'XMLHttpRequest',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        'Content-Type': 'application/x-www-form-urlencoded',
    }

    data = {
        'range': 'TOTAL',
        'explist': '{"QGroup":[{"Items":[{"name":"TH","operate":"=","value":"cell","ExtendType":0},{"name":"ISHAVE_SW","operate":"=","value":"Y"}]}],"S_DBCodes":"TOTAL","OrderBy":"RELEVANT"}',
        'pageIndex': '1',
        'random': '0.7570986898148857'
    }
    proxies = {
        "http": "http://qf:qf3.1415@202.112.118.7:10086/"
    }
    response = requests.post('http://gongjushu.cnki.net/RBook/Search/SearchList', headers=headers, data=data, proxies=proxies)

    allData = formData(response)

    return allData


def index(request):

    data = json.loads(request.POST.get('data', '1'))

    allData= getData(data["values"][0])

    return HttpResponse(json.dumps({
        "collection": allData,
        "allNum": 0
    }), content_type="application/json")